import { reqHomeDB,carList } from '@/api';
//home模块的小仓库
const state = {
    //state中数据默认初始值不能乱写，服务器返回对象/数组，需要根据接口返回值初始化
    carList:{},//购物车数据
    isLogined:true,
    homeDB: {//首页数据
        topicList:[
     
        ],
        categoryList:[
            {
                subCategoryList:[]
            }
        ]
    },//首页数据

};

const actions = { 
    //通过API里面的接口函数调用，向服务器发送请求，获取服务器的数据
    async getHomeDB({commit}){//同步
        let result = await reqHomeDB();//等待请求获取结果         
        commit("GETHOMEDB",result);
    },
    async getCarList({commit}){
        let result = await carList();//等待请求获取结果        
        commit("GETCARLIST",result);
    }
};

const mutations = {
    GETHOMEDB(state,homeDB){
        state.homeDB = homeDB;
    },

    GETCARLIST(state,carList){
        state.carList=carList;
    },
    CHANGEISLOGINED(state,bool){
        state.isLogined=bool;
    }
};

const getters = {
    //宫格
    channel(state){
        return state.homeDB.channel;
    },
    //品牌制造商
    brandList(state){
        return state.homeDB.brandList;
    },
    //新品首发
    newGoodsList(state){
        return state.homeDB.newGoodsList;
    },
    //人气推荐
    hotGoodsList(state){
        return state.homeDB.hotGoodsList;
    },
    //专题精选
    topicList(state){
        return state.homeDB.topicList;
    },
    //商品
    categoryList(state){
        return state.homeDB.categoryList;
    },
    // 专题总数量
    totalPages(state) {
        return state.homeDB.topicList.length;
    },    
    carList(state){
        return state.carList
    }
};


export default {
    state,
    actions,
    mutations,
    getters,
}